package leetcode.bitarithmetic;

import java.util.HashMap;
import java.util.Set;

public class MaximumProductOfWordLengths318 {
    class Solution {
        public int maxProduct(String[] words) {
           HashMap<Integer, Integer> hashMap = new HashMap<>();
           int ans = 0;
           for (String word : words) {
               int size = word.length();
               int mask = 0;
               for (int i = 0; i < size; i++) {
                   mask |= (1 << word.charAt(i) - 'a');
               }
               Set<Integer> keys = hashMap.keySet();
               if (keys.contains(mask)) {
                   hashMap.put(mask, Math.max(hashMap.get(mask), size));
               } else {
                   hashMap.put(mask, size);
               }
               for (int key : keys) {
                   if ((key & mask) == 0) {
                       ans = Math.max(ans, size * hashMap.get(key));
                   }
               }
           }
           return ans;
        }
    }
}
